package com.micro.school.edu.service.impl;

import java.util.List;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.utils.DateUtils;
import com.micro.school.common.security.utils.SecurityUtils;
import com.micro.school.edu.convert.SchoolFeeYearConvert;
import com.micro.school.edu.domain.SchoolFeeYear;
import com.micro.school.edu.domain.vo.SchoolCollegeVo;
import com.micro.school.edu.domain.vo.SchoolFeeYearVo;
import com.micro.school.edu.mapper.SchoolFeeYearMapper;
import com.micro.school.edu.service.ISchoolFeeYearService;
import com.micro.school.system.api.RemoteOrderService;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.vo.SchoolFeeYearVos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import com.micro.school.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 学校收费学年Service业务层处理
 *
 * @author rakic
 * @date 2025-08-07
 */
@Service
public class SchoolFeeYearServiceImpl implements ISchoolFeeYearService
{
    @Autowired
    private SchoolFeeYearMapper schoolFeeYearMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteOrderService remoteOrderService;

    /**
     * 查询学校收费学年
     *
     * @param id 学校收费学年主键
     * @return 学校收费学年
     */
    @Override
    public SchoolFeeYearVo selectSchoolFeeYearById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return schoolFeeYearMapper.selectSchoolFeeYearById(id);
    }

    /**
     * 查询学校收费学年列表
     *
     * @param schoolFeeYearVo 学校收费学年
     * @return 学校收费学年
     */
    @Override
    public List<SchoolFeeYearVo> selectSchoolFeeYearList(SchoolFeeYearVo schoolFeeYearVo)
    {
        schoolFeeYearVo.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        return schoolFeeYearMapper.selectSchoolFeeYearList(schoolFeeYearVo);
    }

    /**
     * 新增学校收费学年
     *
     * @param schoolFeeYearVo 学校收费学年
     * @return 结果
     */
    @Override
    public int insertSchoolFeeYear(SchoolFeeYearVo schoolFeeYearVo)
    {
        SchoolFeeYear schoolFeeYear = SchoolFeeYearConvert.INSTANCE.convert(schoolFeeYearVo);

        schoolFeeYear.setCreateTime(DateUtils.getNowDate());
        schoolFeeYear.setTenantId(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        StudentInfoVos studentInfoVos = remoteUserService.selectSchoolById(Long.valueOf(SecurityContextHolder.getUserTenantId()));
        schoolFeeYear.setSchoolName(studentInfoVos.getSchoolName());
        return schoolFeeYearMapper.insertSchoolFeeYear(schoolFeeYear);

    }

    /**
     * 修改学校收费学年
     *
     * @param schoolFeeYearVo 学校收费学年
     * @return 结果
     */
    @Override
    public int updateSchoolFeeYear(SchoolFeeYearVo schoolFeeYearVo)
    {
        SchoolFeeYear schoolFeeYear = SchoolFeeYearConvert.INSTANCE.convert(schoolFeeYearVo);
        schoolFeeYear.setUpdateTime(DateUtils.getNowDate());
        SchoolFeeYearVo schoolFeeYearVo1 = schoolFeeYearMapper.selectSchoolFeeYearById(schoolFeeYearVo.getId());
        if (!schoolFeeYearVo.getFeeYear().equals(schoolFeeYearVo1.getFeeYear())){
            remoteOrderService.updateAllFeeYear(schoolFeeYearVo.getFeeYear(),schoolFeeYearVo.getId());
        }
        return schoolFeeYearMapper.updateSchoolFeeYear(schoolFeeYear);
    }

    /**
     * 批量删除学校收费学年
     *
     * @param ids 需要删除的学校收费学年主键
     * @return 结果
     */
    @Override
    public int deleteSchoolFeeYearByIds(List<Long> ids)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return schoolFeeYearMapper.deleteBatchIds(ids);
    }

    /**
     * 删除学校收费学年信息
     *
     * @param id 学校收费学年主键
     * @return 结果
     */
    @Override
    public int deleteSchoolFeeYearById(Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return schoolFeeYearMapper.deleteById(id);
    }

    @Override
    public SchoolFeeYearVos selectSchoolFeeYearByIds(Long id) {
        return schoolFeeYearMapper.selectSchoolFeeYearByIds(id,SecurityContextHolder.getUserTenantId());
    }

    @Override
    public List<SchoolFeeYearVos> selectSchoolFeeYearLists(SchoolFeeYearVos schoolFeeYearVos) {

        return schoolFeeYearMapper.selectSchoolFeeYearLists(schoolFeeYearVos);
    }

}
